package com.suxin.exception;

import com.suxin.exception.bussine.Response;
import com.suxin.exception.bussine.ResponseStatus;
import com.suxin.exception.bussine.RestException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Tang
 * @classname CommonExceptionHandler
 * @description [  ]
 * @date 2022/10/12 11:59
 */
@RestControllerAdvice
public class CommonExceptionHandler {

    @Resource
    AbstractExceptionRegister register;

    @ExceptionHandler({Exception.class})
    public Response exceptionHandler(HttpServletRequest request, Exception e) {
        return register.getRuntimeExceptionHandler(e).handlerException(request, e);
    }

    @ExceptionHandler({RestException.class})
    public Response restExceptionHandler(HttpServletRequest request, Exception e) {
        return Response.create().fail(ResponseStatus.FAIL.getCode(), StringUtils.isEmpty(e.getMessage()) ? ResponseStatus.FAIL.getMessage() : e.getMessage());
    }
}
